$(document).ready(function() {
	
	$('#report_diagnosis').autocomplete({
		source: "JsonDiagnosisList",
		minLength: 0
	});			
	
	$('.datepicker').datepicker();
	
	$('#doctor_name').autocomplete({
		source: "JsonUsersList?userClass=d",
		minLength: 1
	});
	
	$('#patient_name').autocomplete({
		source: "JsonUsersList?userClass=all",
		minLength: 1
	});
	
	$('#radiologist_name').autocomplete({
		source: "JsonUsersList?userClass=r",
		minLength: 1
	});	
		
	//These validation rules apply to all forms that edit user data.
	
	$('#edit_user_form').validate({
		rules: {		
			phone: {
				digits: true,
				maxlength: 10,
				minlength: 10
			}
		},
		messages: {
			phone: {
				maxlength:"Enter a valid 10-digit phone number without spaces, dashes or brackets.",
				minlength:"Enter a valid 10-digit phone number without spaces, dashes or brackets.",
				digits:"Enter a valid 10-digit phone number without spaces, dashes or brackets."
			}
		}

	});
	
	$('#add_user_form').validate({
		rules: {
			username: {
				required: true,
				usernameCheck: true
			},
			email: {
				required: true,
				emailCheck: true,
				email: true
			},
			phone: {
				digits: true,
				maxlength: 10,
				minlength: 10
			}
		},
		messages: {
			username: {
				usernameCheck:"This username is already in use."
			},
			email: {
				emailCheck:"This email is already in use."
			},
			phone: {
				maxlength:"Enter a valid 10-digit phone number without spaces, dashes or brackets.",
				minlength:"Enter a valid 10-digit phone number without spaces, dashes or brackets.",
				digits:"Enter a valid 10-digit phone number without spaces, dashes or brackets."
			}			
		}		
	});
	
	//Removes deleted rows from the Users table
	$('.delete_link').click(function() {
		var parent = $(this).parent().parent();
		var user_name = $(this).attr("id");	
		var answer = confirm("Are you sure you want to delete "+user_name+"?");
		if (answer) {
			var string = 'user_name='+user_name;
			$.ajax({
				type: "GET",
				url: "DeleteUser",
				dataType: "text",
				data: string,
				cache: false,
			    beforeSend: function() {
					parent.animate({'backgroundColor':'#fb6c6c'},300);
				},				
				success: function() {
					parent.slideUp(300,function() {
						parent.remove();				
					});
				}
			});

			//return false;
		}
		return false;
	});
	
	//Removes a doctor from the list of doctors
	$('.doctor_remove').click(function() {
		var parent = $(this).parent().parent();
		var doctor = $(this).attr("id");	
		var answer = confirm("Are you sure you want to remove "+doctor+"?");
		if (answer) {
			$.ajax({
				type: "GET",
				url: $(this).attr("href"),
				//dataType: "text",
				//data: string,
				cache: false,
			    beforeSend: function() {
					parent.animate({'backgroundColor':'#fb6c6c'},300);
				},				
				success: function() {
					parent.slideUp(300,function() {
						parent.remove();				
					});
				}
			});

			return false;
		}
	});	
	
	//Removes messages after 5 seconds
	setTimeout(function() {
		$('.message').fadeOut(3000, function() {
			$('.message').remove();
		});
	}, 5000);
});

jQuery.validator.addMethod('usernameCheck', function(username) {
	var postURL = "CheckUsername";
	
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		data:"username=" + username,
		url: postURL,
		success: function(msg) {
			result = (msg=="true") ? true : false;
		}
	});
	return result;
});

jQuery.validator.addMethod('emailCheck', function(email) {
	var postURL = "CheckEmail";
	
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		data:"email=" + email,
		url: postURL,
		success: function(msg) {
			result = (msg=="true") ? true : false;
		}
	});
	return result;
});